#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_set>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<queue>
#include<climits>
#include<stack>

using namespace std;
typedef long long ll;

template<typename T> 
inline void read(T &x){
	x=0;T f=1;char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do x=x*10+ch-'0',ch=getchar();while(ch<='9'&&ch>='0');x*=f;
}
const int maxn=1e5+5;
const int INF=1e9+5;
int val[maxn];
int rnk[maxn];
int ch[maxn][2];
int siz[maxn];
int tot=0;
void pushup(int rt){
  if(rt==0)return;
  siz[rt]=siz[ch[rt][0]]+siz[ch[rt][1]]+1;
}
void dfs(int rt){
  if(rt==0)return;
  dfs(ch[rt][0]);
  cout<<val[rt]<<':'<<siz[rt]<<' ';
  dfs(ch[rt][1]);
}
void split(int rt,int &a,int &b,int v){
  if(rt==0){a=b=0;return ;}
  if(val[rt]<=v){
    a=rt;split(ch[rt][1],ch[rt][1],b,v);
  }else{
    b=rt;split(ch[rt][0],a,ch[rt][0],v);
  }
  pushup(rt);
}
//make sure val of tree a is smaller than tree b
void merge(int &rt,int a,int b){
  if(a==0||b==0){rt=a|b;return;}
  if(rnk[a]<rnk[b]){
    rt=a;merge(ch[rt][1],ch[rt][1],b);
  }else{
    rt=b;merge(ch[rt][0],a,ch[rt][0]);
  }
  pushup(rt);
}

int add(int v){
  ++tot;
  val[tot]=v;
  siz[tot]=1;
  rnk[tot]=rand();
  return tot;
}
void ins(int &rt,int v){
  int x=0,y=0,nn=add(v);
  split(rt,x,y,v);
  merge(x,x,nn);
  merge(rt,x,y);
}
void del(int &rt,int v){
  int x=0,y=0,z=0;
  split(rt,x,y,v);
  split(x,x,z,v-1);
  merge(z,ch[z][0],ch[z][1]);
  merge(x,x,z);
  merge(rt,x,y);
}
int kth(int rt,int k){
  while(siz[ch[rt][0]]+1!=k){
    if(siz[ch[rt][0]]>=k)
      rt=ch[rt][0];
    else{
      k-=siz[ch[rt][0]]+1;
      rt=ch[rt][1];
    }
  }
  return val[rt];
}
int rankof(int &rt,int v){
  int x=0,y=0;
  split(rt,x,y,v-1);
  int ret=siz[x]+1;
  merge(rt,x,y);
  return ret;
}
int pre(int &rt,int v){
  int x=0,y=0;
  split(rt,x,y,v-1);
  int ret=kth(x,siz[x]);
  merge(rt,x,y);
  return ret;
}
int nex(int &rt,int v){
  int x=0,y=0;
  split(rt,x,y,v);
  int ret=kth(y,1);
  merge(rt,x,y);
  return ret;
}
int root;
int main(){
    #ifdef BUG
    freopen("a.in","r",stdin);
    #endif
    int t;
    memset(ch,0,sizeof ch);
    siz[0]=0;
    read(t);
    add(INF);
    root=1;
    int op,num;
    while(t--){
      read(op);read(num);
      if(op==1)ins(root,num);
      else if(op==2)del(root,num);
      else if(op==3)printf("%d\n",rankof(root,num));
      else if(op==4)printf("%d\n",kth(root,num));
      else if(op==5)printf("%d\n",pre(root,num));
      else if(op==6)printf("%d\n",nex(root,num));
      
      //cout<<"done"<<endl;
      //dfs(root);cout<<endl;
    }
}
